﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace PhoneCard.Controllers
{

    [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {

        DAL.TuiUsersDal userDal;
        public string userName = "";
        public AdminController(DAL.TuiUsersDal userDal)
        {
            this.userDal = userDal;
        }


        
        public IActionResult Index()
        {
            List<Entities.TuiUsers> list = userDal.GetListUser("");

            ViewBag.list=list;
            return View();
        }

        public IActionResult UserEdit()
        {
           
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UserAdd(Entities.TuiUsers user)
        {
            //Entities.TuiUsers user = new Entities.TuiUsers();
            if (user.id == 0)
            {
                if (userDal.GetModelByUserName(user.userName) != null)
                {
                    return Json(new { code = 0, msg = "用户已存在！" });
                }
                userDal.Add(user);
                return Json(new { code = 0, msg = "新增成功！" });
            }
            else
            {
                user.edit_time = System.DateTime.Now;
                userDal.Update(user);
                return Json(new { code = 0, msg = "编辑成功！" });
            }

        }
        /// <summary>
        /// 取分页数据，返回 JSON
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IActionResult List()
        {
            //List<Entities.TuiUsers> list = userDal.GetListArray("*", "id desc", pagesize, pageindex,"");

           // ViewBag.list=list;
            return View();
        }

    }
}